/**
 * Created by Kuballle on 10/28/2014.
 */
var MAP = [
    [0, 0, 0, 2, 1, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 1, 0, 0, 0],
    [1, 1, 1, 1, 0, 1, 1, 1, 2],
    [1, 0, 0, 0, 0, 0, 0, 0, 1],
    [2, 1, 1, 1, 0, 1, 1, 1, 1],
    [0, 0, 0, 1, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 0, 1, 0, 0, 0],
    [0, 0, 0, 1, 1, 2, 0, 0, 0]
];

var BOARD = [
    [0, 0, 0, 1, 32, 31, 0, 0, 0],
    [0, 0, 0, 2, 0, 30, 0, 0, 0],
    [0, 0, 0, 3, 0, 29, 0, 0, 0],
    [7, 6, 5, 4, 0, 28, 27, 26, 25],
    [8, 0, 0, 0, 0, 0, 0, 0, 24],
    [9, 10, 11, 12, 0, 20, 21, 22, 23],
    [0, 0, 0, 13, 0, 19, 0, 0, 0],
    [0, 0, 0, 14, 0, 18, 0, 0, 0],
    [0, 0, 0, 15, 16, 17, 0, 0, 0]
];
var MAX = 9;
var CELL = 70;
var TIMEOUTRUN = 400;
var PLAYER = null;
var ROAD = [1, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0, 0, 0, 0];
function Actor(id) {
    this.position;
    this.i;
    this.j;
    this.timer;
    var k, des, randomGift, currentTargetId;
    this.id = this.position = id;
    this.node = document.getElementById("player" + id);
    /**
     * return true if have other player on current player position to a step distance
     * @param {number} step
     */
    var checkPlayersWhenMove = function (step) {
        /**
         * Object contain the check result {nearPosition} : the position of the nearest player
         * {numberObj} : the number of player on this distance
         * @type {{nearPosition: number, numberObj: number}}
         */
        var result = {
            nearPosition : 0,
            numberObj : 0
        };
        for (var i = this.position,length = BOARD.length; i<this.position + step;i++){
            if (i>length){
                if(ROAD[i-length]!==0){
                    result.nearPosition = result.nearPosition ? result.nearPosition : i-length;
                    result.numberObj++;
                }
            }
            if (ROAD[i]!==0) {
                result.nearPosition = result.nearPosition ? result.nearPosition : i;
                result.numberObj++;
            }
        }
        return result;

    };
    this.move = function (str) {
        var step = parseInt(str, 10);
        //console.log(checkPlayersWhenMove.call(this,step).nearPosition +":"+ checkPlayerWhenMove.call(this,step).numberObj);
        var self = this;

        for (k = 0; k < step; k++) {
            setTimeout(function () {
                if (self.position + 1 > ROAD.length) {
                    self.position = 0;
                } else {
                    self.position += 1;
                }
                try {
                    var newTarget = document.getElementById("cell" + self.position);
                    des = newTarget.getBoundingClientRect();
                    self.node.style.left = des.left + "px";
                    self.node.style.top = des.top + "px";
                    randomGift = randomGifts();
//                    switch (randomGift) {
//                        case 0:
//                            newTarget.className = "cell player";
//                            break;
//                        case 1:
//                            newTarget.className = "cell boom";
//                            console.log("boom");
//                            break;
//                        case 2:
//                            newTarget.className = "cell shield";
//                            console.log("shield");
//                            break;
//                        case 3:
//                            newTarget.className = "cell weapons";
//                            console.log("weapons");
//                            break;
//                        case 4:
//                            newTarget.className = "cell mana";
//                            console.log("mana");
//                            break;
//                        case 5:
//                            newTarget.className = "cell blood";
//                            console.log("blood");
//                            break;
//                    }
                    newTarget.className = "cell player";
                    currentTargetId = document.getElementById("cell" + (self.position === 1 ? 32 : (self.position - 1)));
                    if (!document.getElementById("player" + (self.position === 1 ? 32 : (self.position - 1))) || self.position === 2) {
                        currentTargetId.className = "cell diamond";
                    } // when not see another player
                } catch (e) {
                    console.log(e);
                }
            }, k * TIMEOUTRUN);
        }
    };
    return this;
}

function randomGifts() {
    /*
     0:nothing, 1: boom, 2: shield, 3: weapons, 4: mana, 5:blood
     */
    'use strict';
    var gift = [1, 2, 3, 4, 5, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
    return Shuffle(gift)[Math.floor(Math.random() * gift.length)];
}

function Shuffle(o) {
    for (var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
    return o;
}

var currentPlayer;
var playerList = [];

coCaNgua.controller('mainPlay', ['$scope', '$timeout', 'myService', function ($scope, $timeout, myService) {

    'use strict';
    var isBig = false;
    var face = [[-90,90],[0,0],[-180,90],[0,-180],[0,90],[-270,90]];
    switch (myService.get().substr(6,7)){
        case "player1":
            PLAYER = 1;
            break;
        case "player2":
            PLAYER = 2;
            break;
        case "player3":
            PLAYER = 3;
            break;
        case "player4":
            PLAYER = 4;
            break;
    }
    console.log("PLAYER: " +PLAYER);
    $scope.rollingDice = function () {
        $scope.isDisabled = true;
        var diceArray = ['1', '2', '3', '4', '5', '6'];
        //var loopDice = function (i) {
        var dice = diceArray[Math.floor(Math.random() * diceArray.length)];
        document.getElementById('spinner').style.transform = rollface(dice);

        setTimeout(function () {
            currentPlayer.move(dice);
        },1500);
        //$timeout(function () {
            //    if (i < 30) {
            //        loopDice(i + 1);
            //    } else {
            //        document.getElementById('spinner').style.transform=rollface(dice);
            //        document.getElementById('spinner').addEventListener('webkitTransitionEnd',function(){
            //
            //            currentPlayer.move(dice);
            //        },true);
            //        //setTimeout(function(){
            //        //    $scope.isDisabled = false;
            //        //    $scope.$apply();
            //        //},TIMEOUTRUN*$scope.dice);
            //    }
            //}, 100);
        //}
        //loopDice(0);




    };

    generateMap();
    function rollface(x){

        isBig = !isBig;
        return isBig?"rotateX("+(face[x-1][0]+3600)+"deg) rotateY("+(face[x-1][1]+3600)+"deg)":"rotateX("+face[x-1][0]+"deg) rotateY("+face[x-1][1]+"deg)";
    }
    function generateMap() {

        var index = 0;
        for (var i = 0; i < BOARD[0].length; i++) {
            for (var j = 0; j < BOARD[i].length; j++) {
                if (BOARD[i][j]) {
                    var cell = document.createElement("div");
                    var type = "diamond";
                    if (MAP[i][j] === 2) {

                        var cellPlayer = document.createElement("div");
                        cellPlayer.className = "cell " + "player";
                        cellPlayer.id = "player" + BOARD[i][j];

                        cellPlayer.style.top = CELL * i + "px";
                        cellPlayer.style.left = CELL * j + "px";
                        document.getElementById("map").appendChild(cellPlayer);
                        type = "";
                        var player = new Actor(BOARD[i][j]);
                        playerList.push(player);
                    }
                    cell.className = "cell " + type;
                    cell.id = "cell" + BOARD[i][j];

                    cell.style.top = CELL * i + "px";
                    cell.style.left = CELL * j + "px";

                    document.getElementById("map").appendChild(cell);
                }
            }
        }
        currentPlayer = playerList[0];
    }

}]);
